package net.cachedb4j.dao.support;

import net.cachedb4j.exception.database.DatabaseMapException;
import net.cachedb4j.pojo.database.EntityProperties;
import net.cachedb4j.service.CacheService;

import org.apache.log4j.Logger;

public class CacheDaoSupport {
	
	private static Logger logger = Logger.getLogger(CacheDaoSupport.class);
	
	private CacheService cacheService;

	private static int DEFAULT_CACHE_TIME = 1800;

	public void setCacheService(CacheService cacheService) {
		this.cacheService = cacheService;
	}

	public static void setDEFAULT_CACHE_TIME(int default_cache_time) {
		DEFAULT_CACHE_TIME = default_cache_time;
	}
	
	public Object saveEntity(Class<?> clazz, Object entity) throws DatabaseMapException {
		EntityProperties entityProperties = getEntityProperties(clazz);
		if(null == entityProperties) {
			// TODO: try to load again, if fail report error
			logger.error("entity not found!");
			throw new DatabaseMapException();
		} else {
			long id = entityProperties.getMaxId() + 1;
			Object updatedEntity = updateEntity(entity, id);
			cacheService.set(entityProperties.getName() +"_"+ id, DEFAULT_CACHE_TIME , updatedEntity);
			updateEntityList(entityProperties, id);
			updateEntityIndex(entityProperties, updatedEntity);
			return updatedEntity;
		}
	}

	public void updateEntityIndex(EntityProperties entityProperties,
			Object updatedEntity) {
		// TODO Auto-generated method stub
		
	}

	public void updateEntityList(EntityProperties entityProperties, long id) {
		// TODO Auto-generated method stub
		
	}

	public Object updateEntity(Object entity, long id) {
		// TODO Auto-generated method stub
		return null;
	}

	public EntityProperties getEntityProperties(Class<?> clazz) {
		// TODO Auto-generated method stub
		return null;
	}
	
}
